package me.cangming.hotfix001;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.io.IOException;

import me.cangming.cmfixsdk.FileUtils;
import me.cangming.cmfixsdk.Fix;

/**
 * @date 创建时间：2019-09-10
 * @auther cangming
 * @Description 热修复测试
 */
public class SecondActivity extends AppCompatActivity implements View.OnClickListener {

    /**
     * Fix
     */
    private Button mBtnFix;
    /**
     * 计算
     */
    private Button mBtnCalc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        initView();

    }

    private void initView() {
        mBtnFix = (Button) findViewById(R.id.btn_fix);
        mBtnFix.setOnClickListener(this);
        mBtnCalc = (Button) findViewById(R.id.btn_calc);
        mBtnCalc.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.btn_fix:
                File sourceFile = new File(Environment.getExternalStorageDirectory(), "patch_dex.jar");
                File targetFile = new File(getDir("odex", Context.MODE_PRIVATE).getAbsolutePath() + File.separator + "patch.jar");
                if (targetFile.exists()) {
                    targetFile.delete();
                }
                try {

                    FileUtils.copyFile(sourceFile, targetFile);

                    Fix.loadFixedDex(this);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_calc:
                Toast.makeText(SecondActivity.this, "计算结果： " + CalcUtils.calc(), Toast.LENGTH_SHORT).show();
//                Toast.makeText(SecondActivity.this, "修改后计算结果： " + CalcUtils.calc(), Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
